package com.ganbo.ms.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class SocketServer {

    public static void main(String[] args) throws IOException, InterruptedException {

        ExecutorService executorService = Executors.newFixedThreadPool(8);
        ServerSocket serverSocket = new ServerSocket(9988);
        System.out.println("服务端启动成功，等待客户端的链接。。");
        while (true) {
            Socket socket = serverSocket.accept();
            executorService.execute(() -> {
                try {
                    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    System.out.println(Thread.currentThread().getName() + "读取到客户端的内容:" + br.readLine());

                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    bw.write("服务端回复：已经收到你的内容。");

                    bw.newLine();
                    bw.flush();

                    bw.close();
                    br.close();
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }


//        serverSocket.close();
//        System.out.println("服务端执行完毕");
    }
}
